<template>
  <el-card>
    <!-- 面包屑 -->
    <my-bread leavel1="权限管理"
              leavel2="角色列表"></my-bread>
    <!-- 按钮 -->
    <el-row class="addRoleBtn">
      <el-col>
        <el-button type="primary">添加角色</el-button>
      </el-col>
    </el-row>
    <!-- 表格 -->
    <el-table :data="rolelist"
              style="width: 100%">
      <!-- 展开行显示 type="expand" 展开内容template -->
      <el-table-column type="expand"
                       width="200px">
        <template slot-scope="scope">
          <el-row v-for="(item1,i) in scope.row.children"
                  :key="i">
            <el-col :span="4">
              <el-tag @close="delPermission(scope.row.id,item1.id)"
                      type="success"
                      closable>{{item1.authName}}</el-tag>
              <i class="el-icon-arrow-right"></i>
            </el-col>
            <el-col :span="20">
              <el-row v-for="(item2,i) in item1.children"
                      :key="i">
                <el-col :span="4">
                  <el-tag type="warning"
                          @close="delPermission(scope.row.id,item2.id)"
                          closable>{{item2.authName}}</el-tag>
                  <i class="el-icon-arrow-right"></i>
                </el-col>
                <el-col :span="20">
                  <el-tag type="primary "
                          closable
                          @close="delPermission(scope.row.id,item3.id)"
                          v-for="(item3,i) in item2.children"
                          :key="i">{{item3.authName}}</el-tag>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
          <span v-if="scope.row.children.length==0">未分配权限</span>
        </template>
      </el-table-column>
      <el-table-column type="index"
                       label="#"
                       width="200px">
      </el-table-column>
      <el-table-column prop="roleName"
                       label="角色名称"
                       width="200px">
      </el-table-column>
      <el-table-column prop="roleDesc"
                       label="角色描述">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary"
                     size="mini"
                     plain
                     icon="el-icon-edit"
                     @click="showEditUserDia(scope.row)"
                     circle></el-button>
          <el-button type="danger"
                     size="mini"
                     plain
                     @click="showDelUserMsgBox(scope.row.id)"
                     icon="el-icon-delete"
                     circle></el-button>
          <el-button type="success"
                     size="mini"
                     plain
                     icon="el-icon-check"
                     @click="showSetPermissionDia(scope.row)"
                     circle></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 修改权限对话框 -->
    <el-dialog title="修改权限"
               :visible.sync="dialogFormVisiblePer">
      <!-- 树形结构 -->
      <el-tree ref="tree"
               :data="treelist"
               show-checkbox
               node-key="id"
               :default-expanded-keys="arrexpand"
               :default-checked-keys="arraychecked"
               :props="defaultProps">
      </el-tree>

      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogFormVisiblePer = false">取 消</el-button>
        <el-button type="primary"
                   @click="setRolePer()">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      rolelist: [],
      // 修改权限
      dialogFormVisiblePer: false,
      treelist: [],
      defaultProps: {
        children: 'children',
        label: 'authName',
      },
      arrexpand: [],
      arraychecked: [],
    };
  },
  created() {
    this.getRoleList();
  },
  methods: {
    async setRolePer() {
      let arr1 = this.$refs.tree.getCheckedKeys();
    },
    // 展示设置权限对话框
    async showSetPermissionDia(role) {
      // 获取属性结构的权限数据
      const res = await this.$http.get('getpermissiontree');
      console.log(res);
      this.treelist = res.data.permissiontree;
      // 取出所有权限的id放到数组里,循环获取
      var arrtemp1 = [];
      this.treelist.forEach((item1) => {
        arrtemp1.push(item1.id);
        item1.children.forEach((item2) => {
          arrtemp1.push(item2.id);
          item2.children.forEach((item3) => {
            arrtemp1.push(item3.id);
          });
        });
      });
      console.log(arrtemp1);
      this.arrexpand = arrtemp1;
      // 获取所有已有的权限,然后赋值,这里不再写了
      this.arraychecked = arrtemp1;
      this.dialogFormVisiblePer = true;
    },
    async getRoleList() {
      const res = await this.$http.get('getRoleList');
      console.log(res);
      this.rolelist = res.data.peo;
    },
    // 传入角色和权限id
    async delPermission(roleId, permissionId) {
      const res = await this.$http.delete('deletePermission');
      console.log(res);
    },
  },
};
</script>

<style>
.addRoleBtn {
  margin-top: 10px;
}
</style>